
class DBPost{

  constructor(postId){
    this.storageKeyName = 'postList'; // 文章本地缓存存储键值;
    this.postId = postId;
  }

  getPostItemById(){
    var postsData = this.getAllPostData();
    var len = postsData.length;
    for(var i = 0; i < len; i++){
      if(postsData[i].postId == this.postId){
        return{
          //当前文章在缓存数据库数组中的序号
          index: i,
          data: postsData[i]
        }
      }
    }
  }

  //得到全部文章信息
  getAllPostData() {
    var res = wx.getStorageSync(this.storageKeyName);
    if (!res) {
      res = require('../data/data.js').postList;
      this.execSetStorageSync(res);
    }
    return res;
  }

  //保存或更新缓存数据;
  exeSetStorageSync(data) {
    wx.setStorageSync(this.storageKeyName, data)
  }

  // 收藏文章
  collect(){
    return this.updatePostData('collect');
  }

  // 更新本地的点赞、评论信息、收藏、阅读量
  updatePostData(category, newComment){
    var itemData = this.getPostItemById();
    var postData = itemData.data;
    var allPostData = this.getAllPostData();
    
    switch(category){
      case 'collect':
        // 处理收藏
        if(!postData.collectionStatus){
          postData.collectionNum++;
          postData.collectionStatus = true;
        }else{
          postData.collectionNum--;
          postData.collectionStatus = false;
        }
        break;

      case 'up':
        if(!postData.upStatus){
          postData.upNum++;
          postData.upStatus = true;
        }
        else{
          postData.upNum--;
          postData.upStatus = false;
        }
        break;

      case 'comment':
        postData.comments.push(newComment);
        postData.commentNum++;
        break;

      case 'reading':
        postData.readingNum++;
        break;

      default:
        break;
    }

    // 更新缓存数据库
    allPostData[itemData.index] = postData;
    this.exeSetStorageSync(allPostData);
    return postData;
  }

  // 点赞或取消点赞
  up(){
    var data = this.updatePostData("up");
    return data;
  }

  // 获取文章的评论数据
  getCommentData(){
    var util = require('../util/util.js')
    var itemData = this.getPostItemById().data;

    // 按时间降序排列评论
    itemData.comments.sort(this.compareWithTime);
    var len = itemData.comments.length;
    var comment;
    for(var i=0; i<len; i++){
      // 将comment中的时间戳转换成可阅读格式
      comment = itemData.comments[i];
      comment.create_time = util.getDiffTime(comment.create_time, true);
    }

    console.log(itemData.comments)
    return itemData.comments;
  }

  compareWithTime(value1, value2){
    var flag = parseFloat(value1.create_time)-parseFloat(value2.create_time);
    if(flag < 0){
      return 1;
    }
    else if(flag>0){
      return -1;
    }
    else{
      return 0;
    }
  }

  /**发表评论 */
  newComment(newComment){
    this.updatePostData('comment', newComment);
  }

  //阅读量+1;
  addReadingTimes(){
    this.updatePostData('reading');
  }
};

export { DBPost }
